package model;

import dao.Firma_DAO;
import dao.Ordre_DAO;
import dao.Vare_DAO;
import dao.Varegruppe_DAO;
import database.DataBase;
import interfaces.EventInterface;
import interfaces.FirmaInterface;
import interfaces.ListenerInterface;
import interfaces.OrdreInterface;
import interfaces.ServerInterface;
import interfaces.VareInterface;
import interfaces.VaregruppeInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Server extends UnicastRemoteObject implements ServerInterface {

    private ArrayList<ListenerInterface> listeners;
    private DataBase db;

    public Server() throws RemoteException {
        db = DataBase.getInstance();
        listeners = new ArrayList<>();
    }

    @Override
    public boolean login(String username, String password) throws RemoteException {
        boolean result = db.login(username, password);
        EventInterface evt;
        if (result) {
            evt = new ServerEvent("Du er logget ind");
        } else {
            evt = new ServerEvent("Du er ikke logget ind!");
        }
        distributeEvent(evt);
        return result;
    }

    private synchronized void distributeEvent(EventInterface evt) {
        for (int i = 0; i < listeners.size(); i++) {
            try {
                ListenerInterface listener = listeners.get(i);
                listener.actionPerformed(evt);
            } catch (RemoteException ex) {
                System.out.println(ex.getLocalizedMessage());
            }
        }
    }

    @Override
    public void addListener(ListenerInterface listener) throws RemoteException {
        listeners.add(listener);
    }

    @Override
    public void removeListener(ListenerInterface listener) throws RemoteException {
        listeners.remove(listener);
    }
//    @Override
//    public HashMap<String, ArrayList<VareInterface>> getVarer(String username, String firmaname) throws RemoteException {
//         HashMap<String, ArrayList<VareInterface>> vgmap = null;
//        try {
//            VareGrupper_DAO vg = new VareGrupper_DAO();
//            vgmap = vg.getVarer(username, firmaname);
//            System.out.println("fundet varer " + vgmap.size() );
//        } catch (SQLException ex) {
//            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return vgmap; 
//        
//    }

    @Override
    public int opretOrdre(String firma) throws RemoteException {
        int oId = Ordre_DAO.getInstance().opretOrdre(firma);
        return oId;
    }

    @Override
    public void tilføjVareTilOrdre(int OrdreId, String varenavn, String varegruppeNavn, double pris, int antal) throws RemoteException {
        Ordre_DAO.getInstance().tilfoejVarer(OrdreId, varenavn, varegruppeNavn, pris, antal);
    }

    @Override
    public ArrayList<VareInterface> rabatCheck(String firmanavn, String varegruppenavn) throws RemoteException {
        ArrayList<VareInterface> varer = new ArrayList<>();

        Vare_DAO vd = new Vare_DAO();
        try {
            varer = vd.hentVare(firmanavn, varegruppenavn);
            System.out.println("varer " + varer.size());
        } catch (SQLException ex) {
            System.out.println("fejl i hent af varegrupper i dao " + ex.getLocalizedMessage());
        }

        return varer;
    }

    @Override
    public ArrayList<VareInterface> hentVarer(String firma, String varegruppeNavn) throws RemoteException {
        ArrayList<VareInterface> varer = new ArrayList();
        try {
            varer = Varegruppe_DAO.getInstance().hentVarer(firma, varegruppeNavn);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return varer;
    }

    @Override
    public ArrayList<VaregruppeInterface> getVaregrupper() throws RemoteException {
        ArrayList<VaregruppeInterface> varegrupper = new ArrayList<>();

        Varegruppe_DAO vd = new Varegruppe_DAO();
        try {
            varegrupper = vd.hentVaregrupper();
        } catch (SQLException ex) {
            System.out.println("fejl i hent af varegrupper i dao " + ex.getLocalizedMessage());
        }
        return varegrupper;

    }

    @Override
    public ArrayList<VareInterface> hentRabatVare(String firmanavn) throws RemoteException {
        return null;
    }

    @Override
    public void betalOrdre(int ordreId, String firmanavn) throws RemoteException {
        Ordre_DAO ad = new Ordre_DAO();
        ad.betalOrdre(firmanavn, ordreId);
    }

    @Override
    public void ordreModtaget(int ordreId, String firmanavn) throws RemoteException {
        Ordre_DAO ad = new Ordre_DAO();

        System.out.println("før mod " + firmanavn + "   " + ordreId);
        ad.modtagetOrdre(firmanavn, ordreId);

    }

    @Override
    public ArrayList<OrdreInterface> ikkeBetalteOrdre(String firmanavn) throws RemoteException {
        return null;
    }

    @Override
    public double hentFirmaTotalBalance(String firmanavn) throws RemoteException {
        return 0;
    }

    @Override
    public FirmaInterface hentFirma(String username) throws RemoteException {
        Firma_DAO fd = new Firma_DAO();
        FirmaInterface firma = null;
        try {
            firma = fd.hentFirma(username);
        } catch (SQLException ex) {
            System.out.println("ERROR in hent Firma " + ex);
        }

        return firma;
    }

    @Override
    public ArrayList<OrdreInterface> hentAlleOrdre(String firmaname) throws RemoteException {
        Ordre_DAO od = new Ordre_DAO();
        ArrayList<OrdreInterface> alleOrdre = new ArrayList<>();
        System.out.println("er i hent alle ordre ");
        try {
            alleOrdre = od.hentAlleOrdre(firmaname);
        } catch (SQLException ex) {
            System.out.println("fejl i hent af alle ordre dao  " + ex.getLocalizedMessage());
            ex.getStackTrace();
        }

        return alleOrdre;
    }

    @Override
    public ArrayList<OrdreInterface> hentIkkeBetalteOrdre(String firmaname) throws RemoteException {
        Ordre_DAO od = new Ordre_DAO();
        System.out.println("i betalte ordre ");
        ArrayList<OrdreInterface> alleOrdre = new ArrayList<>();
        try {
            System.out.println("lige før");
            alleOrdre = od.hentIkkeBetaltOrdre(firmaname);
            System.out.println("alle ordre " + alleOrdre);
        } catch (SQLException ex) {
            System.out.println("ooohh fejl i ikke betalt ordre dao");
        }

        return alleOrdre;
    }

    @Override
    public ArrayList<OrdreInterface> ikkeModtagedeOrdre(String firmanavn) throws RemoteException {
        Ordre_DAO od = new Ordre_DAO();
        ArrayList<OrdreInterface> alleOrdre = new ArrayList<>();
        try {
            alleOrdre = od.hentIkkeModtagedeOrdre(firmanavn);
        } catch (SQLException ex) {
            System.out.println("ooohh nejfejl i modtagede ordre dao");
        }

        return alleOrdre;

    }

        @Override
    public OrdreInterface hentFuldOrdre(String firmanavn, int ordreid) throws RemoteException {
        Ordre_DAO od = new Ordre_DAO();
        OrdreInterface ordre;
        
        return ordre = od.hentOrdren(firmanavn, ordreid);
    }
    
    
    @Override
    public void lockStart(String varenavn, String varegruppeNavn) throws RemoteException {
        try {
            String sql = "set autocommit = 0";
            db.execute(sql);
            System.out.println(sql);
            sql = "start transaction";
            db.execute(sql);
            System.out.println(sql);
            sql = "select * from vare ";
            sql = sql + "where navn = '" + varenavn + "' ";
            sql = sql + "and vgruppe = '" + varegruppeNavn + "' ";
            sql = sql + "for update";
            db.execute(sql);
            System.out.println(sql);
            System.out.println("/------------------------------------------/");
            System.out.println("/Låser række" + varenavn + " " + varegruppeNavn + "/");
            System.out.println("/------------------------------------------/");
            System.out.println("LÅST!");
        } catch (SQLException ex) {
            System.out.println(ex.getLocalizedMessage());
        }
    }

    @Override
    public void lockEnd() throws RemoteException {
        try {
            String sql = "commit";
            db.execute(sql);
            sql = "unlock tables";
            db.execute(sql);
            sql = "set autocommit = 1";
            db.execute(sql);
        } catch (SQLException ex) {
            System.out.println(ex.getLocalizedMessage());
        }
    }

    @Override
    public void rollback() throws RemoteException {
        try {
            String sql = "rollback";
            db.execute(sql);
        } catch (SQLException ex) {
            System.out.println(ex.getLocalizedMessage());
        }
    }

} // class
